﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class QUNhomQuyenByToChuc_NguoiDung : BusinessBase
    {
        public void SaveData(List<QUNhomQuyenByToChuc_NguoiDung> listInput)
        {
            if (listInput.Count == 0) return;

            foreach (var oItem in listInput)
            {
                this.Context.QUNhomQuyenByToChuc_NguoiDungs.InsertOnSubmit(oItem);
            }
            this.Context.SubmitChanges();
        }

        public QUNhomQuyenByToChuc_NguoiDung GetItem(int pNguoiDungID, int pNhomQuyenByToChucID)
        {
            return this.Context.QUNhomQuyenByToChuc_NguoiDungs.Where(c => c.NguoiDungID == this.NguoiDungID && c.NhomQuyenByToChucID == this.NhomQuyenByToChucID).FirstOrDefault();
        }
        public List<QUNhomQuyenByToChuc_NguoiDung> GetByNguoiDungId(int nguoiDungId)
        {
            return this.Context.QUNhomQuyenByToChuc_NguoiDungs.Where(a => a.NguoiDungID.Equals(nguoiDungId)).ToList();
        }
        public void DeleteAllByNguoiDung(int nguoiDungId)
        { 

        }

        public void Delete()
        {
            this.Context.QUNhomQuyenByToChuc_NguoiDungs.DeleteOnSubmit(GetItem(this.NguoiDungID, this.NhomQuyenByToChucID));
            this.Context.SubmitChanges();
        }
        //Lưu data từ 1 list truyền vào
        public void SaveDataList(int nhanVienId, List<QUNhomQuyenByToChuc_NguoiDung> lstData)
        {
            var query = (from a in this.Context.QUNhomQuyenByToChuc_NguoiDungs
                         where a.NguoiDungID.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                this.Context.QUNhomQuyenByToChuc_NguoiDungs.DeleteAllOnSubmit(query);
                this.Context.SubmitChanges();
            }
            if (lstData.Count > 0)
            {
                foreach (QUNhomQuyenByToChuc_NguoiDung item in lstData)
                {
                    QUNhomQuyenByToChuc_NguoiDung newItem = new QUNhomQuyenByToChuc_NguoiDung();
                    newItem.NguoiDungID = item.NguoiDungID;
                    newItem.NhomQuyenByToChucID = item.NhomQuyenByToChucID;
                    this.Context.QUNhomQuyenByToChuc_NguoiDungs.InsertOnSubmit(newItem);
                }
            }
            this.Context.SubmitChanges();
        }
        public void Save()
        {
            if (this.NhomQuyenByToChucID == 0)
            {
                //addNew
                this.Context.QUNhomQuyenByToChuc_NguoiDungs.InsertOnSubmit(this);
                this.Context.SubmitChanges();
            }
            else
            {

                //edit
                List<QUNhomQuyenByToChuc_NguoiDung> oItem = this.GetByNguoiDungId(this.NguoiDungID).ToList();
                foreach (QUNhomQuyenByToChuc_NguoiDung item in oItem)
                {
                    this.Context.QUNhomQuyenByToChuc_NguoiDungs.DeleteOnSubmit(item);
                }
                QUNhomQuyenByToChuc_NguoiDung oNguoiDung;

                foreach (var item in this.Context.QUNhomQuyenByToChuc_NguoiDungs)
                {
                    oNguoiDung = new QUNhomQuyenByToChuc_NguoiDung();
                    oNguoiDung.NguoiDungID = item.NguoiDungID;
                    oNguoiDung.NhomQuyenByToChucID = item.NhomQuyenByToChucID;

                    oItem.Add(oNguoiDung);
                }

                this.Context.SubmitChanges();
            }
        }
    }
}